<?php

namespace app\admin\controller;

class Monster extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '怪物库',
                'description' => '怪物库模型',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('info'),
                ),
            ),
        );
    }

    public function index()
    {
        $request = $this->request;

        //筛选条件
        $where = [];
        $pageMaps = [];

        $id = $request->post('id');
        $name = $request->post('name');
        $lv = $request->post('lv');

        if ($id) {
            $where[] = ['id','=',$id];
        }
        if ($name) {
            $where[] = ['gname','=',$name];
        }
        if ($lv) {
            $where[] = ['glv','=',$lv];
        }

        //URL参数
        $pageMaps['id'] = $id;
        $pageMaps['name'] = $name;
        $pageMaps['lv'] = $lv;

        $pageAry = [
            'list_rows' => 15,
            'page' => input('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('Monster')->loadList($where, $pageAry,'glv asc,id desc');
        if(!empty($list)){
            foreach ($list as $key=>$val){
                $val['gzb_list'] = $this->strSelect($val['gzb'],1);
                $val['gdj_list'] = $this->strSelect($val['gdj'],2);
                $val['gyp_list'] = $this->strSelect($val['gyp'],3);
            }
        }
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        return $this->fetch('index');
    }

    public function strSelect($strIds,$type): array
    {
        $ary = ($strIds != '' && $strIds != null) ? explode(',', $strIds) : [];
        $list = [];
        if (!empty($ary)) {
            foreach ($ary as $k => $item) {
                $name = '';
                if($type==1){
                    $name = model('Equip')->findValue(['zbid'=>$item],'zbname');
                    $list[$k]['lv'] = model('Equip')->findValue(['zbid'=>$item],'zblv');
                }
                if($type==2){
                    $name = model('Prop')->findValue(['djid'=>$item],'djname');
                }
                if($type==3){
                    $name = model('Drug')->findValue(['ypid'=>$item],'ypname');
                }
                $list[$k]['id'] = $item;
                $list[$k]['name'] = $name;
            }
        }
        return $list;
    }

    public function info()
    {
        $request = request();
        $id = input('id');
        $model = model('Monster');
        if ($request->method() == 'POST') {
            $_POST['gzb'] = empty($_POST['gzb']) ? [] : $this->getAryStr($_POST['gzb']);
            $_POST['gdj'] = empty($_POST['gdj']) ? [] : $this->getAryStr($_POST['gdj']);
            $_POST['gyp'] = empty($_POST['gyp']) ? [] : $this->getAryStr($_POST['gyp']);

            if ($id) {
                $status = $model->editAll($request);
            } else {
                $status = $model->addall($request);
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url' => url('index')]);
            } else {
                returnJson(0, '操作失败');
            }
        } else {
            $info = $model->getInfo($id);
            if (!empty($info)) {
                $info['gzb_list'] = $this->strSelect($info['gzb'],1);
                $info['gdj_list'] = $this->strSelect($info['gdj'],2);
                $info['gyp_list'] = $this->strSelect($info['gyp'],3);
            }
            $this->assign('info', $info);
            //echo "<pre>";print_r($_POST);die;

            $gzb_list = empty($info) ? 1 : count($info['gzb_list']) + 1;
            $this->assign('gzb_list', $gzb_list);
            $gdj_list = empty($info) ? 1 : count($info['gdj_list']) + 1;
            $this->assign('gdj_list', $gdj_list);
            $gyp_list = empty($info) ? 1 : count($info['gyp_list']) + 1;
            $this->assign('gyp_list', $gyp_list);

            $this->fetch('info');
        }
    }

    public function getMoldList()
    {
        $list = [];
        $type = input('type');
        if ($type==1) {
            $list = model('Equip')->field('zbid as id,zbname as name,zblv as lv')->order('zblv asc,zbid desc')->select();
        }
        if ($type==2) {
            $list = model('Prop')->field('djid as id,djname as name')->order('djid desc')->select();
            foreach ($list as $item){
                $item['lv'] = '';
            }
        }
        if ($type==3) {
            $list = model('Drug')->field('ypid as id,ypname as name')->order('ypid desc')->select();
            foreach ($list as $item){
                $item['lv'] = '';
            }
        }
        $this->assign('list', $list);

        return $this->fetch('mold');
    }

    protected function getAryStr($data)
    {
        $ary = [];
        if(!empty($data)){
            foreach($data as $key=>$vo){
                $ary[$key] = $vo['id'];
            }
        }
        return implode(',',$ary);
    }

    /**
     * 删除
     */
    public function del()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        if (model('Monster')->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
}

